package se19team1e.controller;

import java.util.List;

import se19team1e.dao.abstrakt.jaas.CompanyDAO;
import se19team1e.dao.abstrakt.jaas.DepartmentDAO;
import se19team1e.dao.factory.DAOFactory;
import se19team1e.dao.factory.DAOFactorySelector;
import se19team1e.entity.CompanyDTO;
import se19team1e.entity.DepartmentDTO;
import se19team1e.entity.JobAdvertDTO;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.JobAdvertSearchForm;
import se19team1e.gui.ViewJobAdvertForm;
import se19team1e.util.StringUtil;

/**
 * 
 * The view job advert controller class coordinates the operations between the
 * screen, form, and entity objects for viewing job advert.
 * 
 * @author Palaniappan Vairavan
 * 
 */
public class ViewJobAdvertController extends BaseController {

	/**
	 * Constructs the controller.
	 * 
	 * @param application
	 *            Reference to the current application instance.
	 */
	public ViewJobAdvertController(Application application) {
		super(application);
	}

	/**
	 * Initializes the controller.
	 * 
	 * <p>
	 * The delete job advert form is initialized and set as the current active
	 * screen.
	 * </p>
	 * <p>
	 * Initialization involves the retrieval of all companies and departments in
	 * the system.
	 * </p>
	 * 
	 * @throws DataAccessException
	 */
	public void initialize(JobAdvertDTO jobAdvertDTO)
			throws DataAccessException {
		DAOFactory daoFactory = DAOFactorySelector.getDAOFactory();

		// get all companies
		CompanyDAO companyDAO = daoFactory.getCompanyDAO();
		List<CompanyDTO> companies = companyDAO.retrieveAll();

		// get all departments
		DepartmentDAO departmentDAO = daoFactory.getDepartmentDAO();
		List<DepartmentDTO> departments = departmentDAO.retrieveAll();

		// get screen
		ViewJobAdvertForm screen = this.getApplication().getScreen(
				ViewJobAdvertForm.class);

		if (!StringUtil.isEmptyOrNull(jobAdvertDTO.getDepartmentId())) {
			DepartmentDTO departmentDTO = departmentDAO
					.retrieveById(jobAdvertDTO.getDepartmentId());
			jobAdvertDTO.setDepartmentName(departmentDTO.getName());
		}

		if (!StringUtil.isEmptyOrNull(jobAdvertDTO.getCompanyId())) {
			CompanyDTO companyDTO = companyDAO.retrieveById(jobAdvertDTO
					.getCompanyId());
			jobAdvertDTO.setCompanyName(companyDTO.getName());
		}
		// initialize screen
		screen.initialize(jobAdvertDTO, this, companies, departments);

		// set as active screen
		this.getApplication().setCurrentScreen(screen);
	}

	/**
	 * Shows the search job advert form.
	 * 
	 * <p>
	 * This time, only the form is set as the active screen, and no further
	 * initialization is asked to be done.
	 * </p>
	 * <p>
	 * This is called after user clicks on "Ok" on the notification dialog box.
	 * </p>
	 */
	public void showJobAdvertSearchForm() {
		// get screen
		JobAdvertSearchForm screen = this.getApplication().getScreen(
				JobAdvertSearchForm.class);
		// no need to reinitialize, reuse same content.

		// set as active screen
		this.getApplication().setCurrentScreen(screen);
	}

}
